package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.bo.GoodsAddBO;
import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.po.*;
import com.cskaoyan.app.bean.vo.CreateGoodsVo;
import com.cskaoyan.app.mapper.MarketGoodsAttributeMapper;
import com.cskaoyan.app.mapper.MarketGoodsMapper;
import com.cskaoyan.app.mapper.MarketGoodsProductMapper;
import com.cskaoyan.app.mapper.MarketGoodsSpecificationMapper;
import com.cskaoyan.app.service.AdminGoodsService;
import com.cskaoyan.app.util.StringsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class AdminGoodsServiceImpl implements AdminGoodsService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;

    @Override
    public List<MarketGoods> listPageGoods(BasePageInfo basePageInfo, Integer goodsId, String goodsSn, String name) {

        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (goodsId != null) {
            criteria.andIdEqualTo(goodsId);
        }
        if (!StringsUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringsUtil.isEmpty(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (basePageInfo.getLimit() != 0) {
            example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        }
        List<MarketGoods> list = marketGoodsMapper.selectByExample(example);
        return list;

    }


    @Transactional
    @Override
    public void updateGoods(CreateGoodsVo createGoodsVo) {
        List<MarketGoodsProduct> products = createGoodsVo.getProducts();
        List<MarketGoodsAttribute> attributes = createGoodsVo.getAttributes();
        MarketGoods goods = createGoodsVo.getGoods();
        List<MarketGoodsSpecification> specifications = createGoodsVo.getSpecifications();
        Integer id = goods.getId();
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        BigDecimal price=null;
        for (MarketGoodsProduct product : products) {
            price = product.getPrice();
        }
        goods.setRetailPrice(price);
        marketGoodsMapper.updateByExampleSelective(goods, example);

        for (MarketGoodsAttribute attribute : attributes) {
            Integer id1 = attribute.getId();
            if (id1 == null) {
                MarketGoodsAttribute goodsAttribute = new MarketGoodsAttribute();
                goodsAttribute = attribute;
                goodsAttribute.setGoodsId(id);
                goodsAttribute.setAddTime(new Date());
                goodsAttribute.setUpdateTime(new Date());
                goodsAttribute.setDeleted(false);
                marketGoodsAttributeMapper.insertSelective(goodsAttribute);
                continue;

            }
            MarketGoodsAttributeExample example1 = new MarketGoodsAttributeExample();
            MarketGoodsAttributeExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andGoodsIdEqualTo(id);
            criteria1.andIdEqualTo(id1);
            marketGoodsAttributeMapper.updateByExampleSelective(attribute, example1);
        }
        for (MarketGoodsProduct product : products) {
            Integer id1 = product.getId();
            MarketGoodsProductExample example1 = new MarketGoodsProductExample();
            MarketGoodsProductExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andGoodsIdEqualTo(id);
            criteria1.andIdEqualTo(id1);
            marketGoodsProductMapper.updateByExampleSelective(product, example1);
        }
        for (MarketGoodsSpecification specification : specifications) {
            Integer id1 = specification.getId();
            MarketGoodsSpecificationExample example1 = new MarketGoodsSpecificationExample();
            MarketGoodsSpecificationExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andGoodsIdEqualTo(id1);
            criteria1.andIdEqualTo(id1);
            marketGoodsSpecificationMapper.updateByExampleSelective(specification, example1);
        }

    }

    @Override
    public void deleteGoods(MarketGoods marketGoods) {
        MarketGoods goods = new MarketGoods();
        goods.setDeleted(true);
        Integer id = marketGoods.getId();
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        marketGoodsMapper.updateByExampleSelective(goods, example);

    }

    @Override
    @Transactional
    public void createGoods(GoodsAddBO addBO) {
        MarketGoods goods = addBO.getGoods();
        List<MarketGoodsSpecification> specifications = addBO.getSpecifications();
        List<MarketGoodsAttribute> attributes = addBO.getAttributes();
        List<MarketGoodsProduct> products = addBO.getProducts();

        BigDecimal minPrice = products.stream().map(MarketGoodsProduct::getPrice)
                .min(BigDecimal::compareTo).orElse(BigDecimal.ZERO);
        goods.setRetailPrice(minPrice);
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        marketGoodsMapper.insertSelective(goods);
        Integer goodsId = goods.getId();
        assert goodsId != null;
        for (MarketGoodsSpecification specification : specifications) {
            specification.setGoodsId(goodsId);
            specification.setAddTime(new Date());
            specification.setUpdateTime(new Date());
            marketGoodsSpecificationMapper.insertSelective(specification);
        }
        for (MarketGoodsProduct product : products) {
            product.setId(null);
            product.setGoodsId(goodsId);
            product.setAddTime(new Date());
            product.setUpdateTime(new Date());
            marketGoodsProductMapper.insertSelective(product);
        }
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goodsId);
            attribute.setAddTime(new Date());
            attribute.setUpdateTime(new Date());
            marketGoodsAttributeMapper.insertSelective(attribute);
        }
    }
}
